﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using TTData;

namespace Thurn_und_Taxis.components
{
    /// <summary>
    /// Interaction logic for EndGameBox.xaml
    /// </summary>
    public partial class EndGameBox : UserControl
    {
        private static Int32 DEFAULT_HEIGHT = 1024;
        private static Int32 DEFAULT_HEIGHT_BONUS = 38;

        public EndGameBox()
        {
            InitializeComponent();
        }

        public void UpdateScores(List<Player> playersList)
        {
            if (playersList.Count == 0)
                return;

            Int32 score = 0;
            Player p = playersList[0];

            wrapPlayer1.Visibility = Visibility.Visible;
            player1Nickname.Content = p.Nickname;
            for (int i = 0; i < p.Bonuses.Count; i++)
            {
                Image img = GetBonusImage(p.Bonuses[i]);
                player1Bonuses.Children.Add(img);
                score += p.Bonuses[i].Points;
            }
            player1HousesImage.Source = GetHouseImage(p.HouseColor);
            player1HousesImage.Width = 20;

            player1HousesCount.Content = 20 - p.Houses.Count;
            score -= (20 - p.Houses.Count);
            player1Score.Content = "Score = " + score + " pts";


            if (playersList.Count == 1)
                return;
            score = 0;
            wrapPlayer2.Visibility = Visibility.Visible;
            p = playersList[1];
            player2Nickname.Content = p.Nickname;
            for (int i = 0; i < p.Bonuses.Count; i++)
            {
                Image img = GetBonusImage(p.Bonuses[i]);
                player2Bonuses.Children.Add(img);
                score += p.Bonuses[i].Points;
            }
            player2HousesImage.Source = GetHouseImage(p.HouseColor);
            player2HousesImage.Width = 20;

            player2HousesCount.Content = 20 - p.Houses.Count;
            score -= (20 - p.Houses.Count);
            player2Score.Content = "Score = " + score + " pts";





            if (playersList.Count == 2)
                return;
            score = 0;
            wrapPlayer3.Visibility = Visibility.Visible;
            p = playersList[2];
            player3Nickname.Content = p.Nickname;
            for (int i = 0; i < p.Bonuses.Count; i++)
            {
                Image img = GetBonusImage(p.Bonuses[i]);
                player3Bonuses.Children.Add(img);
                score += p.Bonuses[i].Points;
            }
            player3HousesImage.Source = GetHouseImage(p.HouseColor);
            player3HousesImage.Width = 20;

            player3HousesCount.Content = 20 - p.Houses.Count;
            score -= (20 - p.Houses.Count);
            player3Score.Content = "Score = " + score + " pts";




            if (playersList.Count == 3)
                return;
            score = 0;
            wrapPlayer4.Visibility = Visibility.Visible;
            p = playersList[3];
            player4Nickname.Content = p.Nickname;
            for (int i = 0; i < p.Bonuses.Count; i++)
            {
                Image img = GetBonusImage(p.Bonuses[i]);
                player4Bonuses.Children.Add(img);
                score += p.Bonuses[i].Points;
            }
            player4HousesImage.Source = GetHouseImage(p.HouseColor);
            player4HousesImage.Width = 20;

            player4HousesCount.Content = 20 - p.Houses.Count;
            score -= (20 - p.Houses.Count);
            player4Score.Content = "Score = " + score + " pts";

        }

        private Image GetBonusImage(BonusCard card)
        {
            String sourceFile = card.Source;

            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            Image img = new Image();
            img.Height = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
            img.Width = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;

            img.Source = bitmapSource;

            return img;
        }

        private Image GetCarrigeImage(CarrigeCard card)
        {
            try
            {
                Stream imageStreamSource = new FileStream(card.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                Image img = new Image();
                img.Source = bitmapSource;

                return img;
            }
            catch (Exception err)
            {
                return null;
            }            
        }

        private BitmapSource GetHouseImage(String color)
        {
            
            String sourceFile = "";
            if (color.Equals(House.COLOR_BLUE))
                sourceFile = "resources/images/casa_blue.png";
            else if (color.Equals(House.COLOR_RED))
                sourceFile = "resources/images/casa_red.png";
            else if (color.Equals(House.COLOR_YELLOW))
                sourceFile = "resources/images/casa_yellow.png";
            else if (color.Equals(House.COLOR_GREEN))
                sourceFile = "resources/images/casa_green.png";

            FileStream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoderD = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoderD.Frames[0];

            return bitmapSource;
        }
    }
}
